package com.gongpingjialoan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.car.PushMessageBean;

import java.util.List;

/**
 * @Description:
 * @Author: chuang
 * @Since: 2016/9/2 13:51
 * @Version:
 */
public class PushMessageAdapter extends BaseAdapter {

    private List<PushMessageBean> mMessageBean;

    private Context mContext;

    private LayoutInflater mLayoutInflater;

    public PushMessageAdapter(List<PushMessageBean> assessHistories, Context context) {
        mMessageBean = assessHistories;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<PushMessageBean> mMessageBean) {
        this.mMessageBean = mMessageBean;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mMessageBean.size();
    }

    @Override
    public Object getItem(int position) {
        return mMessageBean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.pushmessage_item, null);
            holder = new ViewHolder();
            holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.pic);
            holder.title = (TextView) convertView.findViewById(R.id.message_title);
            holder.content = (TextView) convertView.findViewById(R.id.message_txt);

            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }
        PushMessageBean source = mMessageBean.get(position);
        convertView.setTag(R.id.tag_second, source);
        if (source != null) {
            holder.title.setText(source.title);
            holder.content.setText(source.content);
            Glide.with(mContext).load(source.thumbnail).placeholder(R.drawable.car_loading).centerCrop().crossFade().error(R.drawable.car_no).into(holder.imgThumbnail);
        }

        return convertView;
    }


    public static class ViewHolder {
        public ImageView imgThumbnail;
        public TextView title;
        public TextView content;
    }
}

